package com.zork.cmdb.webapi.service.impl;

import com.github.pagehelper.PageInfo;
import com.zork.cmdb.webapi.common.exception.CmdbException;
import com.zork.cmdb.webapi.domain.dbo.ModuleRelationshipDO;
import com.zork.cmdb.webapi.domain.dto.RelationshipInstanceDTO;
import com.zork.cmdb.webapi.domain.mapper.CustomRelationshipInstanceMapper;
import com.zork.cmdb.webapi.domain.vo.RelationshipInstanceVO;
import com.zork.cmdb.webapi.mapper.cmdb.RelationshipInstanceMapper;
import com.zork.cmdb.webapi.service.ModuleInstanceService;
import com.zork.cmdb.webapi.service.RelationshipInstanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author chenjh
 * @date 2018/5/24
 * @description
 */
@Service
public class RelationshipInstanceImpl implements RelationshipInstanceService {

    @Autowired
    private RelationshipInstanceMapper relationshipInstanceMapper;

    @Autowired
    private CustomRelationshipInstanceMapper customRelationshipInstanceMapper;

    @Autowired
    private ModuleInstanceService moduleInstanceService;

    @Override
    public PageInfo<Map<String, Object>> listRelationshipInstance(Integer moduleID, Integer moduleInstanceID, Integer pageNum, Integer pageSize, String... wheresql) throws CmdbException {
        PageInfo<Map<String, Object>> instanceResult = moduleInstanceService.listInstance(moduleID, moduleInstanceID, pageNum, pageSize, wheresql);
        List<Map<String, Object>> instances = instanceResult.getList();
        if (instances != null && instances.size() > 0) {
            for (Map<String, Object> instanceMap : instances) {
                Integer thisModuleInstanceID = instanceMap.get("moduleInstanceID") == null ? -1 : (Integer) instanceMap.get("moduleInstanceID");
                List<ModuleRelationshipDO> moduleRelationshipDOList = relationshipInstanceMapper.getModuleRelationshipByModuleID(moduleID);
                List<Integer> ralationshipIDList = relationshipInstanceMapper.listRalationshipID(moduleID, thisModuleInstanceID);
                List<RelationshipInstanceDTO> relationshipModuleList = customRelationshipInstanceMapper.getDTOList(moduleRelationshipDOList);
                List<RelationshipInstanceVO> result = customRelationshipInstanceMapper.getVOList(relationshipModuleList);
                for (RelationshipInstanceVO relationship : result) {
                    List<Map<String, Object>> instanceList = new ArrayList<>();
                    if (relationship != null && relationship.getRalationshipID() != null && ralationshipIDList.contains(relationship.getRalationshipID())) {
                        if (moduleID.equals(relationship.getSelectedModuleID())) {
                            StringBuffer sql = moduleInstanceService.getListInstanceSql(relationship.getRalationshipModuleID());
                            sql.append(" AND EXISTS(SELECT 1 FROM [InstanceRelationship] ir WHERE ir.[RalationshipID] = ");
                            sql.append(relationship.getRalationshipID());
                            sql.append(" AND ir.[SelectedInstanceID] = ");
                            sql.append(thisModuleInstanceID);
                            sql.append(" AND m.[ID] = ir.[RelationInstanceID]");
                            sql.append(")");
                            instanceList = relationshipInstanceMapper.listRelationshipInstance(sql.toString());
                        } else if (moduleID.equals(relationship.getRalationshipModuleID())) {
                            StringBuffer sql = moduleInstanceService.getListInstanceSql(relationship.getSelectedModuleID());
                            sql.append(" AND EXISTS(SELECT 1 FROM [InstanceRelationship] ir WHERE ir.[RalationshipID] = ");
                            sql.append(relationship.getRalationshipID());
                            sql.append(" AND ir.[RelationInstanceID] = ");
                            sql.append(thisModuleInstanceID);
                            sql.append(" AND m.[ID] = ir.[SelectedInstanceID]");
                            sql.append(")");
                            instanceList = relationshipInstanceMapper.listRelationshipInstance(sql.toString());
                        }
                    }
                    relationship.setRelationshipInstance(instanceList);
                }
                instanceMap.put("relationshipInstance", result);
            }
        }
        return instanceResult;
    }

}
